#include "BLL.h"
#include "Repository.h"
#include <string.h>
#include <time.h>

//! Products
unsigned int AddProduct(char* Name, unsigned int Quantity)
{
	unsigned int ID = GetAvailableIDForProduct();
	Product NewProduct;

	NewProduct.Name = Name;
	NewProduct.Inventory = Quantity;
	NewProduct.ID = ID;

	SetProduct(NewProduct);

	return ID;
}
unsigned int* SearchProductByName(char* Name)
{
	int i = 0, j = 0, MaxID = GetMaximalIDForProduct();
	unsigned int buffer[1000];

	for (i = 1; i <= MaxID; i++)
	{
		Product p = GetProduct(i);

		if (p.ID)
		{
			if (_strcmpi(p.Name, Name) == 0)
			{
				buffer[j] = p.ID;
				j++;
			}
		}

		free(p.Name);
	}

	unsigned int* result = malloc((j+1)*sizeof(unsigned int));
	buffer[j] = 0;
	memcpy(result, buffer, j+1);


	return result;
}

unsigned int* GetAllProducts()
{
	int i = 0, j = 0, MaxID = GetMaximalIDForProduct();
	unsigned int buffer[1000];

	for (i = 1; i <= MaxID; i++)
	{
		Product p = GetProduct(i);

		if (p.ID)
		{
			
			buffer[j] = p.ID;
			j++;
			
		}

		free(p.Name);
	}

	unsigned int* result = malloc((j + 1)*sizeof(unsigned int));
	buffer[j] = 0;
	memcpy(result, buffer, j + 1);

	return result;
}

//! Regions
unsigned int AddRegion(char* Name)
{
	unsigned int ID = GetAvailableIDForRegion();
	Region NewRegion;

	NewRegion.Name = Name;
	NewRegion.ID = ID;

	SetRegion(NewRegion);

	return ID;
}

unsigned int* GetAllRegions()
{

	int i = 0, j = 0, MaxID = GetMaximalIDForRegion();
	unsigned int buffer[1000];

	for (i = 1; i <= MaxID; i++)
	{
		Region p = GetRegion(i);

		if (p.ID)
		{

			buffer[j] = p.ID;
			j++;

		}

		free(p.Name);
	}

	unsigned int* result = malloc((j + 1)*sizeof(unsigned int));
	buffer[j] = 0;
	memcpy(result, buffer, j + 1);

	return result;
}

unsigned int* SearchRegionsByName(char* Name)
{
	int i = 0, j = 0, MaxID = GetMaximalIDForRegion();
	unsigned int buffer[1000];

	for (i = 1; i <= MaxID; i++)
	{
		Region p = GetRegion(i);

		if (p.ID)
		{
			if (_strcmpi(p.Name, Name) == 0)
			{
				buffer[j] = p.ID;
				j++;
			}
		}

		free(p.Name);
	}

	unsigned int* result = malloc((j + 1)*sizeof(unsigned int));
	buffer[j] = 0;
	memcpy(result, buffer, j + 1);

	return result;
}

int DoesRegionExist(unsigned int ID)
{
	Region checkRegion = GetRegion(ID);

	if (checkRegion.ID)
	{
		free(checkRegion.Name);

		return 1;
	}

	return 0;
}

void DeallocateContact(Contact c)
{
	free(c.ContactInformation);
	free(c.Name);
}

//! Customer
unsigned int AddCustomer(int RegionID, char* Name, char* ContactInformation)
{
	Contact NewContact;
	Customer NewCustomer;

	NewContact.ContactInformation = ContactInformation;
	NewContact.ID = GetAvailableIDForContact();
	NewContact.Name = Name;

	SetContact(NewContact);

	NewCustomer.ContactID = NewContact.ID;
	NewCustomer.ID = GetAvailableIDForCustomer();
	if (DoesRegionExist(RegionID))
		NewCustomer.RegionID = RegionID;
	else
		NewCustomer.RegionID = 0;

	SetCustomer(NewCustomer);

	return NewCustomer.ID;
}

//! Supplier
unsigned int AddSupplier(char* Name, char* ContactInformation)
{
	Contact NewContact;
	Supplier NewSupplier;

	NewContact.ContactInformation = ContactInformation;
	NewContact.ID = GetAvailableIDForContact();
	NewContact.Name = Name;

	SetContact(NewContact);

	NewSupplier.ContactID = NewContact.ID;
	NewSupplier.ID = GetAvailableIDForSupplier();

	SetSupplier(NewSupplier);

	return NewSupplier.ID;
}

unsigned int* SearchSupplierByName(char* Name)
{

}

//! Deliveries
unsigned int AddDeliveryBatch(int CustomerID, Delivery* Array, Time DueDate, int Quantities)
{

}
unsigned int AddSupplyBatch(int SupplierID, Supply* Array, int Quantities)
{

}
unsigned int* GetExpiredDeliveries(Time DueDate)
{

}
unsigned int AddDelivery(unsigned int ProductID, unsigned int Amount) {
	unsigned int ID = GetAvailableIDForDelivery();
	Delivery NewDelivery;

	NewDelivery.Amount = Amount;
	NewDelivery.ProductID = ProductID;
	NewDelivery.ID = ID;

	SetDelivery(NewDelivery);

	return ID;
}
unsigned int AddSupply(unsigned int ProductID, unsigned int Amount) {
	unsigned int ID = GetAvailableIDForSupply();
	Supply NewSupply;

	NewSupply.Amount = Amount;
	NewSupply.ProductID = ProductID;
	NewSupply.ID = ID;

	SetSupply(NewSupply);

	return ID;
}